package servletUtenteReg;

import dominio.Feedback;
import eccezioni.NessunFeedbackException;
import gestioneContesto.GestoreContesto;

import java.io.IOException;
import java.util.ArrayList;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import logicaUtenteReg.GestoreFeedbackRemote;

/**
 * Servlet implementation class ListaFeedbackProfiloServlet
 */
public class ListaFeedbackProfiloServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private RequestDispatcher dispatcher;
	private GestoreContesto contesto;
	private final int ERR = 0;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListaFeedbackProfiloServlet() {
        
    	super();
    	contesto = new GestoreContesto();
    	
    }

    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			eseguiRichiesta(request, response);
		} catch (NamingException e) {
			// TODO Auto-generated catch block--
			e.printStackTrace();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			eseguiRichiesta(request, response);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@SuppressWarnings("deprecation")
	private void eseguiRichiesta(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, NamingException {

		GestoreFeedbackRemote gestore = contesto.getGestore(GestoreFeedbackRemote.class, "GestoreFeedbackRemote");

		String session = request.getParameter("session");
		String nomeId = request.getParameter("nomeId");
		String cognomeId = request.getParameter("cognomeId");
		String utente = request.getParameter("utente");
		int amico = Integer.parseInt(request.getParameter("amico"));

		try {

			ArrayList<Feedback> lista = gestore.listaFeedback(utente);

			for(int i = 0; i < lista.size(); i++) {

				request.setAttribute("mittente" + i, lista.get(i).getMittente());
				request.setAttribute("voto" + i, lista.get(i).getVoto());
				request.setAttribute("giorno" + i, lista.get(i).getData().getDate());
				request.setAttribute("mese" + i, lista.get(i).getData().getMonth());
				request.setAttribute("anno" + i, lista.get(i).getData().getYear());
				request.setAttribute("ore" + i,  lista.get(i).getData().getHours());
				request.setAttribute("minuti" + i, lista.get(i).getData().getMinutes());
				request.setAttribute("secondi" + i, lista.get(i).getData().getSeconds());
				request.setAttribute("commento" + i, lista.get(i).getCommento());
				
			}

			request.setAttribute("size", lista.size());

		} 
		catch (NessunFeedbackException e) {

			request.setAttribute("size", ERR);
			request.setAttribute("msg", e.getMessaggio());

		}
		
		request.setAttribute("session", session);
		request.setAttribute("nomeId", nomeId);
		request.setAttribute("cognomeId", cognomeId);
		request.setAttribute("utente", utente);
		request.setAttribute("amico", amico);
		dispatcher = request.getRequestDispatcher("ListaFeedbackProfilo.jsp");
		dispatcher.forward(request, response);

	}

}
